package com.my.house.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

/**
 * @author yangll
 */
@Data
public class ApiResult<T> {

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    public static final ApiResult<Void> OK = ApiResult.ok();

    private Integer code;

    private String message;

    private T data;

//--------------------------与国际化适配的新方法-----------------------------

    public static <T> ApiResult<T> ok() {
        return of(SUCCESS, "请求完成", null);
    }

    /**
     * 此方法不参与国际化，为兼容方法
     * @param message
     * @return
     */
    public static ApiResult<Void> ok(String message) {
        return of(SUCCESS, message, null);
    }


    public static <T> ApiResult<T> ok(T data) {
        return of(SUCCESS, "请求完成",data);
    }

    public static <T> ApiResult<T> ok(String message, T data) {
        return of(SUCCESS, message, data);
    }

    public static ApiResult<Void> error(String message) {
        return of(ERROR, message, null);
    }

    public static <T> ApiResult<T> of(Integer code, String message, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    @JsonIgnore
    public boolean isSuccessful() {
        return code == 0;
    }

}
